<?php

require_once __DIR__.'/vendor/autoload.php';

// 加载环境变量
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

// 创建数据库连接
try {
    $pdo = new PDO(
        "mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_DATABASE']};charset=utf8mb4",
        $_ENV['DB_USERNAME'],
        $_ENV['DB_PASSWORD'],
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        ]
    );

    echo "✅ 数据库连接成功\n";

    // 检查user_roles表结构
    echo "\n=== user_roles表结构 ===\n";
    $columnsResult = $pdo->query("DESCRIBE user_roles");
    $columns = $columnsResult->fetchAll();

    foreach ($columns as $column) {
        $null = $column['Null'] == 'YES' ? 'NULL' : 'NOT NULL';
        $default = $column['Default'] !== null ? "DEFAULT '{$column['Default']}'" : '';
        echo sprintf("%-20s %-15s %-8s %s\n",
            $column['Field'],
            $column['Type'],
            $null,
            $default
        );
    }

    // 检查user_roles表数据
    echo "\n=== user_roles表数据 ===\n";
    $dataResult = $pdo->query("SELECT * FROM user_roles");
    $data = $dataResult->fetchAll();

    if (empty($data)) {
        echo "❌ user_roles表为空\n";
    } else {
        foreach ($data as $row) {
            echo "ID: {$row['id']}, 用户ID: {$row['user_id']}, 角色ID: {$row['role_id']}\n";
        }
    }

} catch (Exception $e) {
    echo "❌ 错误: " . $e->getMessage() . "\n";
}
